package com.woniuxy.service.emp.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.woniuxy.entity.Emp;
import com.woniuxy.entity.Role;
import com.woniuxy.mapper.custom.CustomEmpMapper;
import com.woniuxy.mapper.custom.CustomRoleMapper;
import com.woniuxy.service.emp.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class EmpServiceImpl implements EmpService {
    @Autowired
    private CustomEmpMapper empMapper;

    @Autowired
    private CustomRoleMapper roleMapper;

    @Override
    public int save(Emp emp) {
        return empMapper.insertSelective(emp);
    }

    @Override
    public int delete(Integer empId) {

        int rows=empMapper.revokeRoleFromEmp(empId);

         rows += empMapper.deleteByPrimaryKey(empId);
        return rows;
    }

    @Override
    public Emp findOne(Integer empId) {
        Emp emp = empMapper.selectByPrimaryKey(empId);
        if (empId == null) {
            throw new RuntimeException("can't find Emp by:" + empId);
        }
        List<Integer> roleIds = empMapper.findRoleIdsByEmpId(empId);

        List<Role> roles = new ArrayList<>();
        for (Integer roleId : roleIds) {
            Role role = roleMapper.selectByPrimaryKey(roleId);
            roles.add(role);
        }

        emp.setRole(roles);

        return emp;
    }

    @Override
    public List<Emp> findAll() {
        return empMapper.selectByExample(null);
    }

    @Override
    public int updateOne(Emp emp) {
        int i = empMapper.updateByPrimaryKey(emp);
        if (i > 0) {
            return i;
        }
        throw new RuntimeException("修改失败");
    }

    @Override
    public int update(Emp emp) {

        return empMapper.updateByPrimaryKeySelective(emp);
    }

    @Override
    public List<Emp> findByLike(String str) {
        if (str == null) {
            throw new RuntimeException("数据获取失败");
        }
        String like = "'%" + str + "%'";
        List<Emp> emps = empMapper.findByLike(like);
        return emps;
    }

    @Override
    public PageInfo<Emp> findByPage(int page, int size) {

        PageHelper.startPage(page, size);

        List<Emp> emps = empMapper.selectByExample(null);
        PageInfo<Emp> info=new PageInfo<>(emps);

        return info;
    }


    @Override
    public List<Emp> Like(String str) {
        List<Emp> list = empMapper.findByLike(str);
        return list;
    }

    @Override
    public int distRoleToEmp(Integer empId, List<Integer> roleIds) {
        int rows = empMapper.revokeRoleFromEmp(empId);

        if (roleIds.size() < 1) {

            return rows;
        }

        rows += empMapper.distRoleToEmp(empId, roleIds);
        return rows;
    }

    @Override
    public int revokeRoleFromEmp(Integer empId) {
        return empMapper.revokeRoleFromEmp(empId);
    }
}
